const { Service } = require('egg');
const ERROR_CODE = require('../errorCode');
const {
  ExperimentPlan,
  ExperimentCourse,
  Laboratory
} = require('../db/model');

class ExperimentPlanService extends Service {
  async getAllExperimentPlan() {
    await ExperimentCourse.hasMany(ExperimentPlan);
    await ExperimentPlan.belongsTo(ExperimentCourse, {
      foreignKey: 'ecId',
    });
    await Laboratory.hasMany(ExperimentPlan);
    await ExperimentPlan.belongsTo(Laboratory, {
      foreignKey: 'lId',
    });
    const res = await ExperimentPlan.findAll({
      attributes: { exclude: ['id', 'ExperimentCourseEcId', 'LaboratoryLId'] },
      include: [ ExperimentCourse, Laboratory ],
    }).catch(err => {
      console.log(`ExperimentPlan 表查找失败: ${err}`);
    });
    if (res === undefined) {
      console.log(`ExperimentPlan 表查找失败: ${ERROR_CODE.DATABASE_ERROR}`);
      return ERROR_CODE.DATABASE_ERROR;
    }
    return res;
  }

  async getExperimentPlan(experimentPlan) {
    if (!(experimentPlan?.ecId && experimentPlan?.epBatch)) {
      console.log(`ExperimentPlan 表查找失败: ${ERROR_CODE.PARAMETERS_MISSING}\n参数: ${experimentPlan}`, experimentPlan);
      return ERROR_CODE.PARAMETERS_MISSING;
    }
    await ExperimentCourse.hasMany(ExperimentPlan);
    await ExperimentPlan.belongsTo(ExperimentCourse, {
      foreignKey: 'ecId',
    });
    await Laboratory.hasMany(ExperimentPlan);
    await ExperimentPlan.belongsTo(Laboratory, {
      foreignKey: 'lId',
    });
    const res = await ExperimentPlan.findOne({
      where: {
        ecId: experimentPlan?.ecId || -1,
        epBatch: experimentPlan?.epBatch || -1,
      },
      attributes: { exclude: ['id', 'ExperimentCourseEcId', 'LaboratoryLId'] },
      include: [ ExperimentCourse, Laboratory ],
    }).catch(err => {
      console.log(`ExperimentPlan 表查找失败: ${err}\n参数: ${experimentPlan}`, experimentPlan);
    });
    if (res === undefined) {
      console.log(`ExperimentPlan 表查找失败: ${ERROR_CODE.DATABASE_ERROR}\n参数: ${experimentPlan}`, experimentPlan);
      return ERROR_CODE.DATABASE_ERROR;
    }
    if (res === null) {
      console.log(`ExperimentPlan 表查找失败: ${ERROR_CODE.PRIMARY_KEY_NOT_EXIST}\n参数: ${experimentPlan}`, experimentPlan);
      return ERROR_CODE.PRIMARY_KEY_NOT_EXIST;
    }
    return res;
  }

  async appendExperimentPlan(experimentPlan) {
    let res = await this.getExperimentPlan(experimentPlan);
    if (res instanceof Object) {
      res = ERROR_CODE.PRIMARY_KEY_EXIST;
    }
    if (res !== ERROR_CODE.PRIMARY_KEY_NOT_EXIST) {
      console.log(`ExperimentPlan 表新增失败: ${res}\n参数: ${experimentPlan}`, experimentPlan);
      return res;
    }
    res = await ExperimentPlan.create(experimentPlan).catch(err => {
      console.log(`ExperimentPlan 表新增失败: ${err}\n参数: ${experimentPlan}`, experimentPlan);
    });
    if (res === undefined) {
      console.log(`ExperimentPlan 表新增失败: ${ERROR_CODE.DATABASE_ERROR}\n参数: ${experimentPlan}`, experimentPlan);
      return ERROR_CODE.DATABASE_ERROR;
    }
    return res;
  }

  async updateExperimentPlan(experimentPlan) {
    let res = await this.getExperimentPlan(experimentPlan);
    if (Object.values(ERROR_CODE).includes(res)) {
      console.log(`ExperimentPlan 表更新失败: ${res}\n参数: ${experimentPlan}`, experimentPlan);
      return res;
    }
    res = await ExperimentPlan.update(experimentPlan, {
      where: {
        ecId: experimentPlan?.ecId || -1,
        epBatch: experimentPlan?.epBatch || -1,
      },
    }).catch(err => {
      console.log(`ExperimentPlan 表更新失败: ${err}\n参数: ${experimentPlan}`, experimentPlan);
    });
    if (res === undefined) {
      console.log(`ExperimentPlan 表更新失败: ${ERROR_CODE.DATABASE_ERROR}\n参数: ${experimentPlan}`, experimentPlan);
      return ERROR_CODE.DATABASE_ERROR;
    }
    return res;
  }

  async destroyExperimentPlan(experimentPlan) {
    let res = await this.getExperimentPlan(experimentPlan);
    if (Object.values(ERROR_CODE).includes(res)) {
      console.log(`ExperimentPlan 表删除失败: ${res}\n参数: ${experimentPlan}`, experimentPlan);
      return res;
    }
    res = await ExperimentPlan.destroy({
      where: {
        ecId: experimentPlan?.ecId || -1,
        epBatch: experimentPlan?.epBatch || -1,
      },
    }).catch(err => {
      console.log(`ExperimentPlan 表删除失败: ${err}\n参数: ${experimentPlan}`, experimentPlan);
    });
    if (res === undefined) {
      console.log(`ExperimentPlan 表删除失败: ${ERROR_CODE.DATABASE_ERROR}\n参数: ${experimentPlan}`, experimentPlan);
      return ERROR_CODE.DATABASE_ERROR;
    }
    return res;
  }
}

module.exports = ExperimentPlanService;
